package com.easepal7506a.project.view;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


import com.easepal7506a.project.R;
import com.example.reslib.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FHB on 2018/5/5.
 */
public class MoveLayout extends LinearLayout {
    private String[] mName;
    private int mCount;
    private Context mContext;
    private double mLlwidth;
    private double mStart;


    private List<Double> mPoint = new ArrayList<>();
    private List<Double> mPointHalf = new ArrayList<>();
    private double mX;
    private int ID = 10000000;
    private int mSize;
    private LinearLayout mLl;
    private List<TextView> mTvList = new ArrayList<>();
    private OnChooseAction mAction;
    private List<Boolean> isChanges = new ArrayList<>();
    private int defaultPosition = 0;
    private boolean isCanMove = false;
    private boolean isNeedUp = false;
    private int margin2;
    private int margin3;
    private int margin4;
    private ScrollView mScroll;

    public MoveLayout(Context context) {
        super(context);
        mContext = context;
    }

    public MoveLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    /**
     * 防止滑动冲突的ScrollView
     * @param s
     */
    public void setScollView(ScrollView s){
        mScroll =s;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mLl = this;
        margin2 = DisplayUtil.dip2px(mContext, -11);
        margin3 =DisplayUtil.dip2px(mContext, -8);
        margin4 =DisplayUtil.dip2px(mContext, -5);


    }

    public void setAction(OnChooseAction action) {
        mAction = action;
    }


    public interface OnChooseAction {
        void onChoose(int position);
    }

    @SuppressLint("ResourceType")
    public void setData(String[] name) {
        mName = name;
        mCount = name.length;
        mLlwidth = DisplayUtil.getScreenWidth(mContext) - DisplayUtil.dip2px(mContext, 70);
        mStart = Double.valueOf(DisplayUtil.dip2px(mContext, 35));
        mSize = mCount;

        for (int i = 0; i < mCount; i++) {
            if (i == 0) {
                mPointHalf.add(mStart + (mLlwidth / (2 * mCount)));
                mPoint.add(mStart + (mLlwidth / mCount));
            } else {
                mPointHalf.add(mPointHalf.get(i - 1) + (mLlwidth / mCount));
                mPoint.add(mPoint.get(i - 1) + (mLlwidth / mCount));
            }
            isChanges.add(false);
            TextView mtv = new TextView(mContext);
            mtv.setId(ID + 1);
            mtv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);



            mtv.setText(mName[i]);
            mtv.setGravity(Gravity.CENTER);
            mtv.setBackgroundResource(R.drawable.background_bottom_choose);
            mtv.setPadding(0, 0, 0, 0);
            LayoutParams lp = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);

            if(mSize ==2){
                if(i==0){
                    lp.setMargins(0, 0, margin2, 0);

                }
                else {
                    lp.setMargins(margin2, 0, 0, 0);

                }
            }
            else if(mSize ==3){
                if(i==0){
                    lp.setMargins(0, 0, margin3, 0);
                }
                else if(i ==2){
                    lp.setMargins(margin3, 0, 0, 0);
                }
                else {
                    lp.setMargins(margin3, 0, margin3, 0);
                }
            }
            else if(mSize ==4){
                if(i==0){
                    lp.setMargins(0, 0, margin4, 0);
                }
                else if(i ==3){
                    lp.setMargins(margin4, 0, 0, 0);
                }
                else {
                    lp.setMargins(margin4, 0, margin4, 0);
                }
            }
            if(i==0){
                mtv.setTextColor(getResources().getColor(R.color.white));
            }
            else {
                mtv.getBackground().setAlpha(0);
                mtv.setTextColor(getResources().getColor(R.color.grey_78));
            }
            mtv.setLayoutParams(lp);

            mTvList.add(mtv);
            this.addView(mtv);
        }



//        for (int i = 0; i < mTvList.size(); i++) {
//            if (i != 0)
//                mTvList.get(i).getBackground().setAlpha(0);
//        }
        doMove();
    }
    public void setTabSelect(int tab){
        Log.e("shunxu","setTabSelect"+tab);
        for(int i = 0;i<mTvList.size();i++){
                if(i==tab){
                    mTvList.get(i).setTextColor(getResources().getColor(R.color.white));
                    mTvList.get(i).getBackground().setAlpha(255);
                }
                else {
                    mTvList.get(i).getBackground().setAlpha(0);
                    mTvList.get(i).setTextColor(getResources().getColor(R.color.grey_78));
                }

        }
    }
    public void setTectColor(int position){
        for(int i =0;i<mTvList.size();i++){
            if(i==position){
                mTvList.get(i).setTextColor(getResources().getColor(R.color.white));
            }
            else {
                mTvList.get(i).setTextColor(getResources().getColor(R.color.grey_78));
            }
        }

    }

    private void doMove() {
        this.setOnTouchListener(new OnTouchListener() {
            private int startY;
            private int startX;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_CANCEL:


                        break;
                    case MotionEvent.ACTION_DOWN:
                        // 防止滑动冲突
                        if(mScroll!=null){
                            mScroll.requestDisallowInterceptTouchEvent(true);
                        }

                        //获取当前按下的坐标
                        isNeedUp = false;
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        if (startX >= mStart && startX < mPoint.get(0)) {
                            defaultPosition = 0;
                        } else if (startX >= mPoint.get(0) && startX < mPoint.get(1)) {
                            defaultPosition = 1;
                        } else if (startX >= mPoint.get(1) && startX < mPoint.get(2)) {
                            defaultPosition = 2;
                        } else if (startX >= mPoint.get(2) && startX < mPoint.get(3)) {
                            defaultPosition = 3;
                        }


                        if (defaultPosition == 0) {
                            for (int i = 0; i < mTvList.size(); i++) {
                                if (i == 0) {
                                    mTvList.get(i).getBackground().setAlpha(255);
                                    mAction.onChoose(0);
                                    mTvList.get(i).setSelected(true);
                                } else {
                                    mTvList.get(i).getBackground().setAlpha(0);
                                    mTvList.get(i).setSelected(false);
                                }
                            }
                            if (startX >= mStart && startX < mPoint.get(0)) {
                                isCanMove = true;
                            } else {
                                isCanMove = false;
                            }
                        } else if (defaultPosition == 1) {
                            for (int i = 0; i < mTvList.size(); i++) {
                                if (i == 1) {
                                    mTvList.get(i).getBackground().setAlpha(255);
                                    mAction.onChoose(1);
                                    mTvList.get(i).setSelected(true);
                                } else {
                                    mTvList.get(i).getBackground().setAlpha(0);
                                    mTvList.get(i).setSelected(false);
                                }
                            }
                            if (startX >= mPoint.get(0) && startX < mPoint.get(1)) {
                                isCanMove = true;
                            } else {
                                isCanMove = false;
                            }
                        } else if (defaultPosition == 2) {
                            for (int i = 0; i < mTvList.size(); i++) {
                                if (i == 2) {
                                    mTvList.get(i).getBackground().setAlpha(255);
                                    mAction.onChoose(2);
                                    mTvList.get(i).setSelected(true);
                                } else {
                                    mTvList.get(i).getBackground().setAlpha(0);
                                    mTvList.get(i).setSelected(false);
                                }
                            }
                            if (startX >= mPoint.get(1) && startX < mPoint.get(2)) {
                                isCanMove = true;
                            } else {
                                isCanMove = false;
                            }
                        } else if (defaultPosition == 3) {
                            for (int i = 0; i < mTvList.size(); i++) {
                                if (i == 3) {
                                    mTvList.get(i).getBackground().setAlpha(255);
                                    mAction.onChoose(3);
                                    mTvList.get(i).setSelected(true);
                                } else {
                                    mTvList.get(i).getBackground().setAlpha(0);
                                    mTvList.get(i).setSelected(false);
                                }
                            }
                            if (startX >= mPoint.get(2) && startX < mPoint.get(3)) {
                                isCanMove = true;
                            } else {
                                isCanMove = false;
                            }
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        // 防止滑动冲突
                        if(mScroll!=null){
                            mScroll.requestDisallowInterceptTouchEvent(true);
                        }
                        //获取移动后的坐标
                        int moveX = (int) event.getRawX();
                        int moveY = (int) event.getRawY();
                        if (Math.abs(moveX - startX) > 20) {
                            isNeedUp = true;
                            //拿到手指移动距离的大小
                            int move_bigX = moveX;
                            mX = Double.valueOf(moveX);
                            startX = moveX;
                            startY = moveY;
                            int move_bigY = moveY;
                            int top1 = mLl.getTop();

                            if (isCanMove && mStart <= moveX && moveX <= (mStart + mLlwidth)) {
                                if (mSize == 4) {
                                    if (moveX >= mStart && moveX <= mPointHalf.get(1)) {
                                        double a1 = ((mPointHalf.get(1) - moveX) / moveX);
                                        if (a1 > 1) {
                                            a1 = 1;
                                        }
                                        if (a1 < 0) {
                                            a1 = 0;
                                        }
                                        double ap1 = 255 * a1;
                                        double ap2 = 255 * (1 - a1);

                                        for (int i = 0; i < mTvList.size(); i++) {
                                            if (i == 0) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap1);
                                                if (ap1 > 200) {
                                                    mAction.onChoose(0);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else if (i == 1) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap2);
                                                if (ap2 > 200) {
                                                    mAction.onChoose(1);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else {
                                                mTvList.get(i).getBackground().setAlpha(0);
                                                mTvList.get(i).setSelected(false);
                                            }
                                        }

                                    } else if (moveX >= mPointHalf.get(1) && moveX <= mPointHalf.get(2)) {
                                        double a1 = ((mPointHalf.get(2) - moveX) / moveX);
                                        if (a1 > 1) {
                                            a1 = 1;
                                        }
                                        if (a1 < 0) {
                                            a1 = 0;
                                        }
                                        double ap1 = 255 * a1;
                                        double ap2 = 255 * (1 - a1);
                                        for (int i = 0; i < mTvList.size(); i++) {
                                            if (i == 1) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap1);
                                                if (ap1 > 200) {
                                                    mAction.onChoose(1);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else if (i == 2) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap2);
                                                if (ap2 > 200) {
                                                    mAction.onChoose(2);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else {
                                                mTvList.get(i).getBackground().setAlpha(0);
                                                mTvList.get(i).setSelected(false);
                                            }
                                        }

                                    } else if (moveX >= mPointHalf.get(2) && moveX <= mStart + mLlwidth) {
                                        double a1 = ((mStart + mLlwidth - moveX) / moveX);
                                        if (a1 > 1) {
                                            a1 = 1;
                                        }
                                        if (a1 < 0) {
                                            a1 = 0;
                                        }
                                        double ap1 = 255 * a1;
                                        double ap2 = 255 * (1 - a1);
                                        for (int i = 0; i < mTvList.size(); i++) {
                                            if (i == 2) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap1);
                                                Log.e("getBackground",ap1+"");
                                                if (ap1 > 200) {
                                                    mAction.onChoose(2);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else if (i == 3) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap2);
                                                if (ap2 > 200) {
                                                    mAction.onChoose(3);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else {
                                                mTvList.get(i).getBackground().setAlpha(0);
                                                mTvList.get(i).setSelected(false);
                                            }
                                        }

                                    }
                                }


                                if (mSize == 3) {
                                    if (moveX >= mStart && moveX <= mPointHalf.get(1)) {
                                        double a1 = ((mPointHalf.get(1) - moveX) / moveX);
                                        if (a1 > 1) {
                                            a1 = 1;
                                        }
                                        if (a1 < 0) {
                                            a1 = 0;
                                        }
                                        double ap1 = 255 * a1;
                                        double ap2 = 255 * (1 - a1);

                                        for (int i = 0; i < mTvList.size(); i++) {
                                            if (i == 0) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap1);
                                                if (ap1 > 200) {
                                                    mAction.onChoose(0);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else if (i == 1) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap2);
                                                if (ap2 > 200) {
                                                    mAction.onChoose(1);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else {
                                                mTvList.get(i).getBackground().setAlpha(0);
                                                mTvList.get(i).setSelected(false);
                                            }
                                        }

                                    } else if (moveX >= mPointHalf.get(1) && moveX <= mStart + mLlwidth) {
                                        double a1 = ((mStart + mLlwidth - moveX) / moveX);
                                        if (a1 > 1) {
                                            a1 = 1;
                                        }
                                        if (a1 < 0) {
                                            a1 = 0;
                                        }
                                        double ap1 = 255 * a1;
                                        double ap2 = 255 * (1 - a1);
                                        for (int i = 0; i < mTvList.size(); i++) {
                                            if (i == 1) {
                                                if (ap1 > 200) {
                                                    mAction.onChoose(1);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                                mTvList.get(i).getBackground().setAlpha((int) ap1);
                                            } else if (i == 2) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap2);
                                                if (ap2 > 200) {
                                                    mAction.onChoose(2);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else {
                                                mTvList.get(i).getBackground().setAlpha(0);
                                                mTvList.get(i).setSelected(false);
                                            }
                                        }
//
                                    }
                                }

                                if (mSize == 2) {
                                    if (moveX >= mStart && moveX <= mStart + mLlwidth) {
                                        double a1 = ((mStart + mLlwidth - moveX) / moveX);
                                        if (a1 > 1) {
                                            a1 = 1;
                                        }
                                        if (a1 < 0) {
                                            a1 = 0;
                                        }
                                        double ap1 = 255 * a1;
                                        double ap2 = 255 * (1 - a1);
                                        for (int i = 0; i < mTvList.size(); i++) {
                                            if (i == 0) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap1);
                                                if (ap1 > 200) {
                                                    mAction.onChoose(0);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else if (i == 1) {
                                                mTvList.get(i).getBackground().setAlpha((int) ap2);
                                                if (ap2 > 200) {
                                                    mAction.onChoose(1);
                                                    mTvList.get(i).setSelected(true);
                                                }
                                            } else {
                                                mTvList.get(i).getBackground().setAlpha(0);
                                                mTvList.get(i).setSelected(false);
                                            }
                                        }

                                    }

                                }
                            }


                            //拿到当前控件未移动的坐标
                            int left = mLl.getLeft();
                            int top = mLl.getTop();
                            left += move_bigX;
                            top += move_bigY;
                            int right = left + mLl.getWidth();
                            int bottom = top + mLl.getHeight();
//                        mLl.layout(left, top, right, bottom);

                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        // 防止滑动冲突
                        if(mScroll!=null){
                            mScroll .requestDisallowInterceptTouchEvent(false);
                        }
                        Log.e("UPUPUP",mX+"==="+(Math.abs(mX - startX)));
                        if(isNeedUp) {
                            if (mSize == 4) {
                                if (mX <= mPoint.get(0)) {
                                    for (int i = 0; i < mTvList.size(); i++) {
                                        if (i == 0) {
                                            mTvList.get(i).getBackground().setAlpha(255);
                                            mAction.onChoose(i);
                                            mTvList.get(i).setSelected(true);
                                        } else {
                                            mTvList.get(i).getBackground().setAlpha(0);
                                            mTvList.get(i).setSelected(false);
                                        }
                                    }

                                } else if (mPoint.get(0) < mX && mX <= mPoint.get(1)) {
                                    for (int i = 0; i < mTvList.size(); i++) {
                                        if (i == 1) {
                                            mTvList.get(i).getBackground().setAlpha(255);
                                            mAction.onChoose(i);
                                            mTvList.get(i).setSelected(true);
                                        } else {
                                            mTvList.get(i).getBackground().setAlpha(0);
                                            mTvList.get(i).setSelected(false);
                                        }
                                    }
                                } else if (mPoint.get(1) < mX && mX <= mPoint.get(2)) {
                                    for (int i = 0; i < mTvList.size(); i++) {
                                        if (i == 2) {
                                            mTvList.get(i).getBackground().setAlpha(255);
                                            mAction.onChoose(i);
                                            mTvList.get(i).setSelected(true);
                                        } else {
                                            mTvList.get(i).getBackground().setAlpha(0);
                                            mTvList.get(i).setSelected(false);
                                        }
                                    }
                                } else if (mPoint.get(2) < mX ) {
                                    for (int i = 0; i < mTvList.size(); i++) {
                                        if (i == 3) {
                                            mTvList.get(i).getBackground().setAlpha(255);
                                            mAction.onChoose(i);
                                            mTvList.get(i).setSelected(true);
                                        } else {
                                            mTvList.get(i).getBackground().setAlpha(0);
                                            mTvList.get(i).setSelected(false);
                                        }
                                    }
                                }
                            }
                            if (mSize == 3) {
                                if (mX <= mPoint.get(0)) {
                                    for (int i = 0; i < mTvList.size(); i++) {
                                        if (i == 0) {
                                            mTvList.get(i).getBackground().setAlpha(255);
                                            mAction.onChoose(i);
                                            mTvList.get(i).setSelected(true);
                                        } else {
                                            mTvList.get(i).getBackground().setAlpha(0);
                                            mTvList.get(i).setSelected(false);
                                        }
                                    }

                                } else if (mPoint.get(0) < mX && mX <= mPoint.get(1)) {
                                    for (int i = 0; i < mTvList.size(); i++) {
                                        if (i == 1) {
                                            mTvList.get(i).getBackground().setAlpha(255);
                                            mAction.onChoose(i);
                                            mTvList.get(i).setSelected(true);
                                        } else {
                                            mTvList.get(i).getBackground().setAlpha(0);
                                            mTvList.get(i).setSelected(false);
                                        }
                                    }
                                } else if (mPoint.get(1) < mX ) {
                                    for (int i = 0; i < mTvList.size(); i++) {
                                        if (i == 2) {
                                            mTvList.get(i).getBackground().setAlpha(255);
                                            mAction.onChoose(i);
                                            mTvList.get(i).setSelected(true);
                                        } else {
                                            mTvList.get(i).getBackground().setAlpha(0);
                                            mTvList.get(i).setSelected(false);
                                        }
                                    }
                                }

                            }

                            if (mSize == 2) {
                                if (mX <= mPoint.get(0)) {
                                    for (int i = 0; i < mTvList.size(); i++) {
                                        if (i == 0) {
                                            mTvList.get(i).getBackground().setAlpha(255);
                                            mAction.onChoose(i);
                                            mTvList.get(i).setSelected(true);
                                        } else {
                                            mTvList.get(i).getBackground().setAlpha(0);
                                            mTvList.get(i).setSelected(false);
                                        }
                                    }

                                } else if (mPoint.get(0) < mX && mX <= mStart + mLlwidth) {
                                    for (int i = 0; i < mTvList.size(); i++) {
                                        if (i == 1) {
                                            mTvList.get(i).getBackground().setAlpha(255);
                                            mAction.onChoose(i);
                                            mTvList.get(i).setSelected(true);
                                        } else {
                                            mTvList.get(i).getBackground().setAlpha(0);
                                            mTvList.get(i).setSelected(false);
                                        }
                                    }
                                }

                            }
                        }
                        break;

                }
                return true;//此处一定要返回true，否则监听不生效
            }
        });
    }

}
